package com.whacos.modules.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whacos.common.annotations.Log;
import com.whacos.common.controller.BaseController;
import com.whacos.common.enums.Constant;
import com.whacos.common.enums.DelFlagEnum;
import com.whacos.common.utils.BeanUtils;
import com.whacos.common.utils.DateUtils;
import com.whacos.common.utils.StringUtils;
import com.whacos.common.vo.ResponseVO;
import com.whacos.modules.blog.entity.Comment;
import com.whacos.modules.blog.service.CommentService;
import com.whacos.modules.blog.vo.CommentVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiongdun
 * @description 文章内容
 * @since 2018/11/17 17:11
 */
@Controller
@RequestMapping("/blog/comment")
@Slf4j
public class CommentController extends BaseController {
    @Autowired
    private CommentService commentService;

    @GetMapping()
    @RequiresPermissions("blog:comment:comment")
    public String comment() {
        return "blog/comment/comment";
    }

    // @Log("查询博客评论内容列表")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("blog:comment:comment")
    public IPage<CommentVO> list(CommentVO commentVO) {
        IPage<CommentVO> commentVOPage = new Page<>();
        try {

            IPage<Comment> commentPage = commentService.page(new Page<>(commentVO.getPageNum(), commentVO.getPageSize()),
                    new LambdaQueryWrapper<Comment>()
                            .eq(StringUtils.isNotEmpty(commentVO.getContent()), Comment::getContent, commentVO.getContent())
                            .orderByDesc(Comment::getUpdatedDate));

            BeanUtils.copyPage(commentPage, commentVOPage);
            List<CommentVO> commentVOList = new ArrayList<>();
            commentPage.getRecords().forEach(source -> {
                CommentVO target = new CommentVO();
                BeanUtils.copyProperties(source, target);
                commentVOList.add(target);
            });
            commentVOPage.setRecords(commentVOList);
        } catch (Exception e) {
            log.error("查询博客评论列表信息异常，异常原因：" + e.getMessage(), e);
            return null;
        }
        return commentVOPage;
    }

    // @Log("新增博客评论内容")
    @GetMapping("/add")
    @RequiresPermissions("blog:comment:add")
    public String add() {
        return "blog/comment/add";
    }

    // @Log("跳转到修改博客评论内容")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("blog:comment:edit")
    public String edit(@PathVariable("id") Long id, Model model) {
        Comment comment = commentService.getById(id);
        model.addAttribute("comment", comment);
        return "blog/comment/edit";
    }

    @Log("保存博客评论内容")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("blog:comment:add")
    public ResponseVO save(CommentVO commentVO) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentVO, comment);
            comment.createdData(this.getUserId());
            comment.setDelFlag(DelFlagEnum.NORMAL.getValue());
            if (this.commentService.save(comment)) {
                return ResponseVO.success().put("id", comment.getId());
            }
        } catch (Exception e) {
            return ResponseVO.error();
        }
        return ResponseVO.success();
    }

    @Log("修改博客评论内容")
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("blog:comment:edit")
    public ResponseVO update(CommentVO commentVO) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentVO, comment);
            comment.updatedData(this.getUserId());
            if (commentService.updateById(comment)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            return ResponseVO.error();
        }
        return ResponseVO.success();
    }

    @Log("删除博客评论内容")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("blog:comment:remove")
    public ResponseVO remove(@RequestParam(value = "id") Long id) {
        try {
            if (Constant.DEMO_ACCOUNT.equals(this.getUsername())) {
                return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
            }
            Comment comment = new Comment();
            comment.setId(id);
            comment.updatedData(this.getUserId());
            comment.setDelFlag(DelFlagEnum.DELETE.getValue());
            if (commentService.updateById(comment)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            return ResponseVO.error();
        }
        return ResponseVO.success();
    }

    @Log("批量删除博客评论内容")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("blog:comment:batchRemove")
    public ResponseVO batchRemove(@RequestParam("ids[]") List<Long> ids) {
        if (Constant.DEMO_ACCOUNT.equals(this.getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            // 封装批量更新数据
            List<Comment> commentList = new ArrayList<>();
            ids.forEach(id -> {
                Comment comment = new Comment();
                comment.setId(id);
                comment.updatedData(this.getUserId());
                comment.setDelFlag(DelFlagEnum.DELETE.getValue());
                commentList.add(comment);
            });

            if (commentService.updateBatchById(commentList)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            return ResponseVO.error();
        }
        return ResponseVO.success();
    }
}
